package AcceptanceTests;

import forum.server.controller.ForumFacade;
import forum.server.controller.ForumFacadeInt;
import forum.server.domain.Post;
import forum.server.domain.PostContent;
import forum.server.exceptions.DatabaseException;
import forum.server.exceptions.RegistrationException;
import forum.server.persistence.DataManipulatorProxy;

public class Bridge {
	private ForumFacadeInt forum;
	//the bridge saves the last post that was added to the system during testing.
	private Post post;
	/**
	 * Constructor for the bridge.
	 */
	public Bridge(){
		this.post=null;
		try{
			DataManipulatorProxy.getInstance().resetDataBase();
			this.forum=new ForumFacade();
		}
		catch(DatabaseException e){
			System.out.println("DataBase problem please try again later.");
		}
	}
	/**
	 * @param username the chosen username for the new user.
	 * @param password password to for the new user.
	 * @return true id successful, false otherwise.
	 */
	public boolean register(String username, String password){
		try{
			this.forum.register(username, password);
			return true;
		}
		catch(DatabaseException e){
			System.out.println(e.getMessage());
			return false;
		}
		catch(RegistrationException e){
			e.toString();//just so the variable will be used.
			return false;
		}
	}
	/**
	 * This function checks if username is the last to log in the system
	 * during the tests.
	 * @param username the user name to check if logged in to the system.
	 * @return true if username is the last user logged in to the system
	 * during the tests, false otherwise.
	 */
	public boolean checkLoggedIn(String username){
		return (this.forum.getLoggedUser()!=null &&
				this.forum.getLoggedUser().getUsername().equals(username));
	}
	/**
	 * This logoff function removes the last user (from the test) from the system.
	 * @param username the user name who wants to logon.
	 * @return true if the user was logged on false otherwise.
	 * @throws DatabaseException 
	 */
	public boolean logoff(String username) throws DatabaseException{
		
		if(checkLoggedIn(username)){
			this.forum.logoff();
			return true;
		}
		else return false;
	}
	/**
	 * this function tries to add msg to the forum.
	 * @param msg the message to add to the forum.
	 * @return true if successful, false otherwise.
	 */
	public boolean addPost(String msg){
		try{
			this.post=this.forum.addPost("testing",new PostContent(msg));
			return true;
		}
		catch(Exception e){
			e.getMessage();
			return false;
		}
	}
	
	public boolean addPost(String header, String msg){
		try{
			this.post=this.forum.addPost(header ,new PostContent(msg));
			return true;
		}
		catch(Exception e){
			e.getMessage();
			return false;
		}
	}
	
	/**
	 * login to the system.
	 * @param username the username of the user to log in.
	 * @param password the password to login.
	 * @return true if successful, false otherwise.
	 */
	public boolean login(String username,String password){
		try{
			this.forum.login(username, password);
			return true;
		}
		catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}
	}
	/**
	 * getter for the last post that was added to the system during tetsing.
	 * @return the last post added to the system.
	 */
	public Post getLastPost() {
		return this.post;
	}
	/**
	 * @return the author of the last post that was added during tesing.
	 */
	public String getAuthorOfLastPostAdded(){
		return this.post.getAuthor();
	}
	/**
	 * @return the content of the last post that was added during tesing.
	 */
	public String getContentOfLastPostAdded(){
		return this.post.getContent().getContent();
	}

	/**
	 * @return the amount of posts in the system.
	 */
	public int getPostsCount(){
		try{
			return DataManipulatorProxy.getInstance().getPosts().size();
		}
		catch(DatabaseException e){
			System.out.println("DataBase problem please try again later.");
			return 0;
		}
	}
	
	public String searchByAuthor(String username, int from, int to){
		try{
			return this.forum.searchByAuthor(username, from, to);
		}
		catch (Exception e){
			System.err.println("Error occured while testing.");
			return null;
		}
	}
	
	public String searchByContent(String phrase, int from, int to){
		try{
			return this.forum.searchByContent(phrase, from, to);
		}
		catch (Exception e){
			System.err.println("Error occured while testing.");
			return null;
		}
	}
}
